package com.example.service;

import com.example.common.CustomException;
import com.example.common.ServiceCode;
import com.example.common.ServiceException;
import com.example.dao.UserInfoDao;
import com.example.dto.UserPageDTO;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.example.entity.UserInfo;
import com.example.common.ResultCode;
import com.example.vo.UserInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.crypto.SecureUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class UserInfoService {

    @Resource
    private UserInfoDao userInfoDao;

    /**
     * 查询所有的消费者
     * @return
     */
    public List<UserInfo> findByLevel3(){
        return userInfoDao.findByLevel3();
    }

    public UserInfo add(UserInfo userInfo) {
        // 唯一校验
        int count = userInfoDao.checkRepeat("name", userInfo.getName(), null);
        if (count > 0) {
            throw new CustomException("1001", "用户名\"" + userInfo.getName() + "\"已存在");
        }
        if (StringUtils.isEmpty(userInfo.getPassword())) {
            // 默认密码123456
            userInfo.setPassword(SecureUtil.md5("123456"));
        } else {
            userInfo.setPassword(SecureUtil.md5(userInfo.getPassword()));
        }
        userInfoDao.insertSelective(userInfo);
        return userInfo;
    }

    public void delete(Long id) {
        userInfoDao.deleteByPrimaryKey(id);
    }

    public void update(UserInfo userInfo) {
        userInfoDao.updateByPrimaryKeySelective(userInfo);
    }

    public UserInfo findById(Long id) {
        return userInfoDao.selectByPrimaryKey(id);
    }

    public List<UserInfoVo> findAll() {
        return userInfoDao.findByName("all");
    }

    public PageInfo<UserInfoVo> findPage(String name, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfoVo> all = userInfoDao.findByName(name);
        return PageInfo.of(all);
    }

    public UserInfoVo findByUserName(String name) {
        return userInfoDao.findByUsername(name);
    }

    public UserInfo login(String username, String password) {
        UserInfo userInfo = userInfoDao.findByUsername(username);
        if (userInfo == null) {
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        if (!SecureUtil.md5(password).equalsIgnoreCase(userInfo.getPassword())) {
            throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
        }
        return userInfo;
    }

    /**
     * 更新用户信息
     * @param userInfo
     */
    public void updateUser(UserInfo userInfo) {
        this.userInfoDao.updateByPrimaryKeySelective(userInfo);
    }

    /**
     * 分页查询所有消费者信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<UserInfo> findByLevel3Page(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfo> byLevel3 = this.findByLevel3();
        // 使用 PageInfo 来处理分页信息
        PageInfo<UserInfo> pageInfo = new PageInfo<>(byLevel3);

        // 检查请求的页码是否超出范围
        if (pageNum <= 0 || pageNum > pageInfo.getPages()) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "页码超出范围");
        }
        return pageInfo;
    }

    /**
     * 查询（分页）
     * 包括模糊查询
     * @param userPageDTO
     * @return
     */
    public PageInfo<UserInfo> findAmbiguous(UserPageDTO userPageDTO) {
        Integer pageIndex = userPageDTO.getPageIndex();
        Integer pageSize = userPageDTO.getPageSize();
        PageHelper.startPage(pageIndex, pageSize);

        Integer level = userPageDTO.getLevel();
        String name = userPageDTO.getName();
        String sex = userPageDTO.getSex();
        List<UserInfo> all = this.userInfoDao.findAmbiguous(name, sex, level);
        // 使用 PageInfo 来处理分页信息
        PageInfo<UserInfo> pageInfo = new PageInfo<>(all);

        // 检查请求的页码是否超出范围
        if (pageIndex <= 0 || pageIndex > pageInfo.getPages()) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "页码超出范围");
        }

        return pageInfo;
    }

    /**
     * 根据权限等级查询所有用户
     * @param level
     * @return
     */
    public List<UserInfo> getByLevel(Integer level) {
        List<UserInfo> all = this.userInfoDao.getByLevel(level);
        return all;
    }
}
